/**
 * 
 */
package com.demo2do.boyle.backend.service.aduser.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.boyle.backend.service.aduser.AduserRefundService;
import com.demo2do.boyle.common.entity.aduser.Aduser;
import com.demo2do.boyle.common.entity.aduser.AduserBalance;
import com.demo2do.boyle.common.entity.aduser.order.PayType;
import com.demo2do.boyle.common.entity.aduser.refund.AduserRefund;
import com.demo2do.boyle.common.entity.aduser.refund.RefundStatus;
import com.demo2do.boyle.common.entity.aduser.transaction.AduserTransaction;
import com.demo2do.boyle.common.entity.aduser.transaction.TransactionType;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.service.impl.GenericServiceImpl;
import com.demo2do.core.web.resolver.Page;

/**
 * @author Leo
 *
 */
@Service("aduserRefundService")
public class AduserRefundServiceImpl extends GenericServiceImpl<AduserRefund> implements AduserRefundService {

	@Autowired 
	private GenericDaoSupport genericDaoSupport;
	
	/*
	 * (non-Javadoc)
	 * @see com.demo2do.boyle.backend.service.aduser.AduserRefundService#list(com.demo2do.core.web.resolver.Page)
	 */
	@SuppressWarnings("unchecked")
	public List<AduserRefund> list(Page page) {
		List<AduserRefund> aduserRefund = genericDaoSupport.searchForList("FROM AduserRefund WHERE refundStatus = :refundStatus", "refundStatus", RefundStatus.AUDITING, page.getBeginIndex(), page.getMaxResultRecords());
		page.calculate(aduserRefund.size());
		return aduserRefund.size() > page.getEveryPage() ? aduserRefund.subList(0, page.getEveryPage()) : aduserRefund;
	}

	/* (non-Javadoc)
	 * @see com.demo2do.boyle.backend.service.aduser.AduserRefundService#audit(com.demo2do.boyle.common.entity.aduser.refund.AduserRefund)
	 */
	public void audit(AduserRefund aduserRefund) {
		
		int balance = aduserRefund.getBalance();
		
		// 1. update aduser_refund 
		genericDaoSupport.update(aduserRefund.audit());
		
		// 2. update aduser balance
		Aduser aduser = genericDaoSupport.load(Aduser.class, aduserRefund.getAduser().getId());
		genericDaoSupport.update(aduser.refund(balance));
		
		// 3. update aduser_balance balance and refundTotal
		AduserBalance aduserBalance = genericDaoSupport.load(AduserBalance.class, aduser.getId());
		genericDaoSupport.update(aduserBalance.refund(balance));
		
		// 4. create aduser_transaction
		AduserTransaction AduserTransaction = new AduserTransaction(aduser, TransactionType.REFUND, PayType.INTERNAL);
		genericDaoSupport.save(AduserTransaction.initialize(balance, aduserRefund.getId()));
	}

	@Override
	public int count(RefundStatus status) {
		Map<String, Object> parameters = new HashMap<String, Object>(1);
		parameters.put("refundStatus", status);
		return genericDaoSupport.searchForInt("SELECT COUNT(1) FROM AduserRefund WHERE 1=1 "+ (status == null? "" : " and refundStatus = :refundStatus "), parameters);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AduserRefund> list(RefundStatus status, Page page) {
		List<AduserRefund> aduserRefund = genericDaoSupport.searchForList("FROM AduserRefund WHERE 1=1 "+ (status == null? "" : " and refundStatus = :refundStatus "), "refundStatus",  status, page.getBeginIndex(), page.getMaxResultRecords() );
		page.calculate(aduserRefund.size());
		return aduserRefund.size() > page.getEveryPage() ? aduserRefund.subList(0, page.getEveryPage()) : aduserRefund;
	}
	
}
